package org.okeoe.gen.ctl;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.okeoe.gen.entity.CgGroupEntity;
import org.okeoe.gen.entity.dto.CgGroupDTO;
import org.okeoe.gen.services.CgGroupAccessServices;
import org.okeoe.gen.services.CgGroupConfigServices;
import org.okeoe.gen.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Title: 模板分组 mybatis dao
 * @author dj
 * @date 2019-10-28 14:08:44
 * @version V1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/cgGroupCtl")
@Api(tags ="模板分组")
public class CgGroupCtl {
   @Autowired
   private CgGroupAccessServices cgGroupAccessServicesImpl;
   @Autowired
   private CgGroupConfigServices cgGroupConfigServicesImpl;

   /**
   *  分页查询
   * @param pageNum 起始页
   * @param pageSize 大小
   * @return 分页数据
   * @throws Exception
   */
   @GetMapping("/pageList")
   @ApiOperation(value = "模板分组"+ BaseMsg.MODULAR_PAGE,notes ="模板分组"+ BaseMsg.MODULAR_PAGE)
   public PageInfo<CgGroupDTO> pageCgGroupList(Integer pageNum, Integer pageSize, CgGroupDTO dto, HttpServletRequest request) throws Exception{
        return cgGroupAccessServicesImpl.pageList(pageNum,pageSize,dto);

   }

    /**
    * 保存
    * @param entityName
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/add")
    @ApiOperation(value = "模板分组"+ BaseMsg.MODULAR_ADD,notes = "模板分组"+BaseMsg.MODULAR_ADD)
    public ResultMsg addCgGroup(@Validated({AddView.class}) CgGroupEntity entityName, HttpServletRequest request) throws Exception{
        return cgGroupConfigServicesImpl.insertSelective(entityName);
    }
    /**
    * 更新
    * @param updateEntityDto
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/update")
    @ApiOperation(value = "模板分组"+ BaseMsg.MODULAR_UPDATE,notes = "模板分组"+ BaseMsg.MODULAR_ADD)
    public ResultMsg updateCgGroup(@Validated({UpdateView.class}) CgGroupEntity updateEntityDto,HttpServletRequest request) throws Exception{
        CgGroupEntity entity = cgGroupAccessServicesImpl.selectById(updateEntityDto.getId());
        ObjectCopyUtils.copyPropertiesIf(updateEntityDto,entity, BaseConstant.UPDATE);
        return cgGroupConfigServicesImpl.updateSelectiveById(entity);
    }

    /**
    * 根据id查询
    * @param id
    * @return 查询对象
    * @throws Exception
    */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "模板分组"+ BaseMsg.MODULAR_FIND_ID,notes = "模板分组"+BaseMsg.MODULAR_FIND_ID)
    public ResultMsg findCgGroupById(String id) throws Exception{
        CgGroupEntity entityName = cgGroupAccessServicesImpl.selectById(id);
         return ResultMsg.success(entityName);
    }

     /**
     * 删除
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/delById")
     @ApiOperation(value = "模板分组"+BaseMsg.MODULAR_DEL,notes ="模板分组"+BaseMsg.MODULAR_DEL)
     public ResultMsg delCgGroupById(String ids) throws Exception{
       return cgGroupConfigServicesImpl.deleteByIds(ids.split(BaseConstant.SPOT));
     }

}